package a09_贪心算法;

/**
 * <p>
 * a04_最大子序和复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/2/10
 */
public class a04_最大子序和复习2 {
    public static void main(String[] args) {
        int i = maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4});
        System.out.println();
    }
    public static int maxSubArray(int[] nums) {
        //如果和为负数，重新计数
        int maxResult = Integer.MIN_VALUE;
        int result = 0;
        for (int i = 0; i < nums.length; i++) {
            if(result < 0){
                result = 0;
            }
            result+=nums[i];
            maxResult = Math.max(result, maxResult);
        }
        return maxResult;
    }
}
